package com.lxh.lxhcrm.controller.customer;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxh.lxhcrm.model.pojos.*;
import com.lxh.lxhcrm.model.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    CustomerService cusService;
    /**
     * 查询所有客户信息（带分页）
     * @param search: 模糊查询搜索值
     * @param cusTypeName：客户类型名
     * @param stageId：客户阶段名:
     * @return: 客户对象集合
     */

    @RequestMapping("/find-cusinfo")
    public Map<String,Object> findAllCusInfo(Integer currPage, Integer pageSize,String search,Integer cusTypeName,Integer stageId){
        if (currPage == null || currPage == 0){ currPage = 1;}
        if (pageSize == null || pageSize == 0){ pageSize = 5;}
        Page<Object> page = PageHelper.startPage(currPage, pageSize);
        Map<String,Object> res = new HashMap<>(3);
        List<Customer> customers = cusService.allCusInfo(search,cusTypeName,stageId);
        res.put("data",customers);
        res.put("total",page.getTotal());
        return res;
    }
    /**
     * 查询所有客户信息（不带分页）
     * @return 客户对象集合
     */
    @RequestMapping("/select_cus")
    public List<Customer>  select_cus(){return cusService.select_cus();};
    /**
     * 查询所有客户阶段
     * @return 客户阶段集合
     */
    @RequestMapping("/all_stage")
    public List<CusStage> allStage(){return cusService.allStage();};
    /**
     * 查询所有客户类型
     * @return 客户类型集合
     */
    @RequestMapping("/all-custype")
    public List<CustomerType> allCusType(){
        return cusService.allCusType();
    };

    /**
     * 查询所有客户来源
     * @return 客户来源集合
     */
    @RequestMapping("/all_cussource")
    public List<CustomerSource> allCusSource(){return cusService.allCusSource();};

    /**
     * 查询所有公海
     * @return 公海对象集合
     */
    @RequestMapping("/all_sea")
    public List<Sea> allSea(){return cusService.allSea();};

    /**
     * 查询所有联系人（带分页）
     * @param currPage: 当前页
     * @param pageSize：页大小
     * @return 联系人对象集合
     */
    @RequestMapping("/all_linkman")
    public Map<String, Object> all_linkman(Integer currPage, Integer pageSize){
        if (currPage == null || currPage == 0){ currPage = 1;}
        if (pageSize == null || pageSize == 0){ pageSize = 5;}

        Page<Object> pa = PageHelper.startPage(currPage, pageSize);
        Map<String,Object> result = new HashMap<>(3);
        List<Contact> contacts = cusService.all_linkman();
        result.put("data2",contacts);
        result.put("total2",pa.getTotal());
        System.out.println(pa.getTotal());
        return result;
    }
    /**
     * 查询所有联系人类型
     * @return集合
     */
    @RequestMapping("/all_contact_type")
    public  List<ContactType> all_contact_type(){return  cusService.all_contact_type();};
    /**
     * 查询某客户所有联系人
     * @param customerId
     * @return 该客户所有联系人
     */
    @RequestMapping("/hisLinkMan")
    public  List<Contact> hisLinkman(Integer customerId){return cusService.hisLinkman(customerId);};
    /**
     * 查询所有客户行为标签
     * @return 行为对象
     */
    @RequestMapping("/all_behaviors")
    public List<Behavior> allBehaviors(){return  cusService.allBehaviors();}

    /**
     * 查询某客户所有相关员工行动记录(带分页)
     * @param customerId
     * @return 该客户所有行动记录集合
     */
    @RequestMapping("/all_actionrec")
    public List<ActionRecord> allActionRecor(Integer customerId){return cusService.allActionRecor(customerId);};
    /**时间线
     *
     * @param customerId
     * @return 时间线所需所有数据
     */
    @RequestMapping("/timeline")
    public List<Map<String,Object>> timeLine(Integer customerId){return  cusService.timeLine(customerId);};
    /**
     * 查询某客户所有合同
     * @param customerId
     * @return 该客户所有合同集合
     */
    @RequestMapping("all_hisContracts")
    public List<Contract> allHisContracts(Integer customerId){return cusService.allHisContracts(customerId);};
    /**
     * 查询某客户所有销售机会
     * @param customerId
     * @return 该客户所有机会集合
     */
    @RequestMapping("/his_saleoppo")
    public  List<Opportunity> hisSaleOppo(Integer customerId){return  cusService.hisSaleOppo(customerId);};

    /**
     * 查询某客户行为记录
     * @param customerId
     * @return 客户行为记录集合
     */
    @RequestMapping("/action_score")
    public List<BehaviorRecord> actionScore(Integer customerId){return  cusService.actionScore(customerId);};
    /**
     * 成交客户统计
     * */
    @RequestMapping("/static_data")
    public List<Map<String,Object>> staticData(Integer index){return cusService.staticData(index);};

    /**
     * 客户销售统计
     * @return
     */
    @RequestMapping("/static_sale")
    public List<Map<String,Object>> saleStatic(){return  cusService.saleStatic();};


/*------------------------------添加-----------------------------------*/
    /**
     * 添加客户信息（主从表添加）
     * @param customer
     */
    @RequestMapping("/add_cus_data")
    public void add_cus_data(@RequestBody Customer customer){
        cusService.add_cus_data(customer);
    }
    /**
     * 添加联系人信息
     * @param contact
     */
    @RequestMapping("/add_contact")
    public void add_contact(@RequestBody Contact contact) {
        cusService.add_contact(contact);
    }
    /**
     * 添加行动记录
     * @param acrec
     */
    @RequestMapping("/add_actionRecord")
    public void addActionRecord(@RequestBody ActionRecord acrec){
        System.out.println("从前端传来的员工行动记录"+ acrec);
        cusService.addActionRecord(acrec);
    }
    /**
     * 添加客户行为记录
     * @param bere
     */
    @RequestMapping("/add_beRe")
    public void addBehaviorRecord(@RequestBody BehaviorRecord bere){cusService.addBehaviorRecord(bere); }
}